﻿using UnityEngine;
using System.Collections;
using UnityEditor;

//------------------------------------------------------------
// shaco Framework
// Copyright © 2017-2021 chang.liu All rights reserved.
// Feedback: 449612236@qq.com
//------------------------------------------------------------

namespace shacoEditor
{
    public class UICreateaterHierarchy
    {
        // [MenuItem("GameObject/UI/RichText")]
        // static public void CreateRichText()
        // {
        //     CreateUIInScene<shaco.RichTextComponent>();
        // }

        [MenuItem("GameObject/UI/ListView")]
        static public void CreateListView()
        {
            var script = CreateUIInScene<shaco.ListViewComponent>();
            script.CheckCompoment();
        }

        [MenuItem("GameObject/UI/ScrollBarEx", false)]
        static public void CreateListViewScrollBarEx()
        {
            var selectGameObject = Selection.activeGameObject;
            var newObj = new GameObject();
            newObj.name = "ScrollBar";
            var background = newObj.AddComponent<UnityEngine.UI.Image>();
#if UNITY_5_3_OR_NEWER
            background.raycastTarget = false;
#endif
            background.color = Color.black;
            newObj.transform.SetParent(selectGameObject.transform, false);

            var script = CreateUIInScene<shaco.ScrollBarComponent>(newObj);
            script.name = "ScrollBarValue";
            script.handleRect = script.GetComponent<RectTransform>();
#if UNITY_5_3_OR_NEWER
            script.gameObject.AddComponent<UnityEngine.UI.Image>().raycastTarget = false;
#endif            
            script.Background = background;

            var listView = selectGameObject.GetComponent<shaco.ListViewComponent>();
            script.AutoSetDirection(listView);
        }

        [MenuItem("GameObject/UI/ScrollBarEx", true)]
        static public bool CreateListViewScrollBarExValid()
        {
            return Selection.activeGameObject != null && Selection.activeGameObject.GetComponent<shaco.ListViewComponent>() != null;
        }

        [MenuItem("GameObject/UI/NumberLoopScrollAction")]
        static public void CreateNumberLoopScrollAction()
        {
            CreateUIInScene<shaco.NumberLoopScrollActionComponent>();
        }

        static private T CreateUIInScene<T>(GameObject parent = null) where T : UnityEngine.Component
        {
            T retValue = default(T);
            if (Selection.activeGameObject != null)
            {
                var newObj = new GameObject();

                if (null != parent)
                    newObj.transform.SetParent(parent.transform, false);
                else
                    newObj.transform.SetParent(Selection.activeGameObject.transform, false);
                retValue = newObj.AddComponent<T>();
                newObj.name = typeof(T).Name;
                Selection.activeGameObject = newObj;

                EditorHelper.SetDirty(Selection.activeGameObject);
            }
            return retValue;
        }
    }
}

